#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname tools $PACKAGE) || exit 1;
PKG_VERSION=$(pkgversion tools $PACKAGE) || exit 2;
PKG_ARCHIVE=$(pkgarchive tools $PACKAGE) || exit 3;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE) || exit 4;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
sed -i 's/groups$(EXEEXT) //' src/Makefile.in && \
	find man -name Makefile.in -exec sed -i 's/groups\.1 / /' {} \;
	sed -i	-e 's@#ENCRYPT_METHOD DES@ENCRYPT_METHOD SHA512@' \
			-e 's@/var/spool/mail@/var/mail@' etc/login.defs && \
	sed -i 's/1000/999/' etc/useradd


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
./configure --sysconfdir=/etc --with-group-name-max-length=32 \
    --prefix=/tools \
    && \
    make && \
    make DESTDIR=$LFS install



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
echo "Installed $PACKAGE"
echo " --OK--"
